﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTCash.Logger;
using Uranus.Service.PaymentService;
using Uranus.Service.UserService;
using BaseResponse = Uranus.Service.PaymentService.BaseResponse;

namespace Uranus.Service
{
    public class Payment : BaseData
    {
        private static Payment payment = null;

        public static Payment GetPayment()
        {
            if (payment == null)
            {
                payment = new Payment();
            }
            return payment;
        }

        /// <summary>
        /// 投标回调
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public BuyCallBackResponse BuyCallBack(BuyCallBackRequest request)
        {
            PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
            try
            {
                return paymentServiceClient.BuyCallBack(request);
            }
            catch (Exception ex)
            {
                string parmsStr = Common.CodeCommon.Serialize(request);
                LogHelper.WriteLogError("投标回调WCF异常 BuyCallBack", parmsStr, ex);
                return new BuyCallBackResponse();
            }
            finally
            {
                paymentServiceClient.Close();
            }
        }

        /// <summary>
        /// 充值回调
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ChargeCallBackResponse ChargeCallBack(ChargeCallBackRequest request)
        {
            PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
            try
            {
                return paymentServiceClient.ChargeCallBack(request);
            }
            catch (Exception ex)
            {
                string parmsStr = Common.CodeCommon.Serialize(request);
                LogHelper.WriteLogError("充值回调WCF异常 ChargeCallBack", parmsStr, ex);
                return new ChargeCallBackResponse();
            }
            finally
            {
                paymentServiceClient.Close();
            }
        }

        /// <summary>
        /// 提现回调
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public CashCallBackResponse CashCallBack(CashCallBackRequest request)
        {
            PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
            try
            {
                return paymentServiceClient.CashCallBack(request);
            }
            catch (Exception ex)
            {
                string parmsStr = Common.CodeCommon.Serialize(request);
                LogHelper.WriteLogError("提现回调WCF异常 CashCallBack", parmsStr, ex);
                return new CashCallBackResponse();
            }
            finally
            {
                paymentServiceClient.Close();
            }
        }

        /// <summary>
        /// 根据ID获取用户交易记录
        /// </summary>
        /// <param name="userTransferId"></param>
        /// <returns></returns>
        public Uranus.Service.PaymentService.UserTransferResponse GetUserTransferById(int userTransferId)
        {
            PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
            try
            {
                return paymentServiceClient.GetUserTransferById(userTransferId);
            }
            catch (Exception ex)
            {
                string parmsStr = Common.CodeCommon.Serialize(userTransferId);
                LogHelper.WriteLogError("根据ID获取用户交易记录 WCF异常 GetUserTransferById", parmsStr, ex);
                return new Uranus.Service.PaymentService.UserTransferResponse();
            }
            finally
            {
                paymentServiceClient.Close();
            }
        }

        /// <summary>
        /// 解绑银行卡
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public UnbundlingCallBackResponse UnbundlingCallBack(UnbundlingCallBackRequest request)
        {
            PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
            try
            {
                return paymentServiceClient.UnbundlingCallBack(request);
            }
            catch (Exception ex)
            {
                string parmsStr = Common.CodeCommon.Serialize(request);
                LogHelper.WriteLogError("解绑回调WCF异常 UnbundlingCallBack", parmsStr, ex);
                return new UnbundlingCallBackResponse();
            }
            finally
            {
                paymentServiceClient.Close();
            }
        }

        /// <summary>
        /// 修复提现先成功后失败的处理接口
        /// </summary>
        /// <param name="userTransferId"></param>
        /// <returns></returns>
        public BaseResponse RepairCashResultFailTransfer(int userTransferId)
        {
            PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
            try
            {
                return paymentServiceClient.RepairCashResultFailTransfer(userTransferId);
            }
            catch (Exception ex)
            {
                string parmsStr = Common.CodeCommon.Serialize(userTransferId);
                LogHelper.WriteLogError("修复提现先成功后失败的处理接口异常 RepairCashResultFailTransfer", parmsStr, ex);
                return new BaseResponse();
            }
            finally
            {
                paymentServiceClient.Close();
            }
        }
    }
}
